﻿using IOA.MES.Client.Common;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOA.MES.Client.Framing
{
    public class SugarFactory
    {

        //从配置文件中读取连接字符串
        private static string dbConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        //从配置文件中读取连接字符串 表明，列名
        private static string TableName = ConfigurationManager.ConnectionStrings["TableName"].ConnectionString;
        private static string ColumnName = ConfigurationManager.ConnectionStrings["ColumnName"].ConnectionString;

        /// <summary>
        /// 封装SugarClient
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetInstance()
        {
            if (string.IsNullOrEmpty(dbConnectionString))
            {
                throw new Exception("数据库连接字符串未找到");
            }
            SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = dbConnectionString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (property, column) =>
                    {
                        var attributes = property.GetCustomAttributes(true);//get all attributes 

                        if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
                        {
                            column.IsPrimarykey = true; //有哪些特性可以看 1.2 特性明细
                        }
                        //可以写多个，这边可以断点调试
                        if (attributes.Any(it => it is NotMappedAttribute))
                        {
                            column.IsIgnore = true;
                        }
                    },
                    EntityNameService = (type, entity) =>
                    {
                        var attributes = type.GetCustomAttributes(true);
                        if (attributes.Any(it => it is TableAttribute))
                        {
                            entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name;
                        }
                    }
                }
            }, db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    CommonMessage.Log(LogLevel.Info, sql + " " + pars + "\r\n");
                };
            });
            return Db;
        }
        /// <summary>
        /// 查询条码是否存在
        /// </summary>
        /// <param name="textCode"></param>
        /// <returns></returns>
        public static bool QueryBarCode(string textCode)
        {
            var db = GetInstance();
            var dt = db.Ado.GetDataTable("select * from @tableName where  @columnName = @barCode",
                    new { tableName = TableName, columnName = ColumnName, barCode = textCode });
            return dt.DataSet.Tables.Count > 0;
        }
    }



   

}
